var channelForeClose;
function request(paras) {
	var url = location.href;
	if (url.lastIndexOf("#") > -1)
		url = url.substring(0, url.lastIndexOf("#"));

	var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
	var paraObj = {}
	for (i = 0; j = paraString[i]; i++) {
		paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j
						.indexOf("=")
						+ 1, j.length);
	}
	var returnValue = paraObj[paras.toLowerCase()];
	if (typeof(returnValue) == "undefined") {
		return "";
	} else {
		return decodeURIComponent(returnValue);
	}
}

define(['../../js/libex/custom2.0'], function (){
	var $;
	//var channelForeClose;
	/**
	 * 页面展示前数据解密以及时间戳校验
	 */

	function myInitMethod(l, j){
		initPage(l, request('param'));
	}

	
	
	
	var browser = {	// 根据userAgent判断是否为微信客户端
		getAgent : function() {
			var b = navigator.userAgent.toLowerCase();
			return b;
		},
		isWeiXin : function() {
			var g = navigator.userAgent.toLowerCase();
			var h = "MicroMessenger";
			var f = "iphone";
			var e = true;
			h = h.toLowerCase();
			if (g.indexOf(h) > 0 || g.indexOf(f) > 0) {
				e = true;
			} else {
				e = false;
			}
			return e;
		}
	};
	var storeData = new Object();
	var loadPwd = false;

	function isIdCardNo(b) {	// 验证卡号规则
		b = b.toUpperCase();
		if (!(/(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/.test(b))) {
			return false;
		}
		return true;
	}
	function UUID() {	// 生成UUID
		this.id = this.createUUID();
	}
	UUID.prototype.valueOf = function() {
		return this.id;
	};
	UUID.prototype.toString = function() {
		return this.id;
	};
	UUID.prototype.createUUID = function() {
		var c = new Date(1582, 10, 15, 0, 0, 0, 0);
		var f = new Date();
		var h = f.getTime() - c.getTime();
		var i = UUID.getIntegerBits(h, 0, 31);
		var g = UUID.getIntegerBits(h, 32, 47);
		var e = UUID.getIntegerBits(h, 48, 59) + "2";
		var b = UUID.getIntegerBits(UUID.rand(4095), 0, 7);
		var d = UUID.getIntegerBits(UUID.rand(4095), 0, 7);
		var a = UUID.getIntegerBits(UUID.rand(8191), 0, 7)
				+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)
				+ UUID.getIntegerBits(UUID.rand(8191), 0, 7)
				+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)
				+ UUID.getIntegerBits(UUID.rand(8191), 0, 15);
		return i + g + e + b + d + a;
	};
	UUID.getIntegerBits = function(f, g, b) {
		var a = UUID.returnBase(f, 16);
		var d = new Array();
		var e = "";
		var c = 0;
		for (c = 0; c < a.length; c++) {
			d.push(a.substring(c, c + 1));
		}
		for (c = Math.floor(g / 4); c <= Math.floor(b / 4); c++) {
			if (!d[c] || d[c] == "") {
				e += "0";
			} else {
				e += d[c];
			}
		}
		return e;
	};
	UUID.returnBase = function(c, d) {
		var e = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
				"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
				"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
		var b=null;
		if (c < d) {
			b = e[c];
		} else {
			var f = "" + Math.floor(c / d);
			var a = c - f * d;
			if (f >= d) {
				b = this.returnBase(f, d) + e[a];
			} else {
				b = e[f] + e[a];
			}
		}
		return b;
	};
	UUID.rand = function(a) {
		return Math.floor(Math.random() * a);
	};
	

	Array.prototype.indexOf = function(val) {
		for (var i = 0; i < this.length; i++) {

			if (this[i] == val)
				return i;
		}
		return -1;
	};

	Array.prototype.remove = function(val) {
		var index = this.indexOf(val);
		if (index > -1) {
			this.splice(index, 1);
		}
	};

	

	function deleteMenu(){
		WeixinApi.ready(function(api){
			api.hideOptionMenu();
			});
	};
	return window.framework_ready = function (param) {
		var g = CBUI.Component;
		var f = CBUI.Browser;
		var h = CBUI.Fn;
		$ = h.$;
		var paramValue = request('param');
		MSG.loadMsg();

		g.TimeOut = g.Page.extend({
					onSubmit : function(a, b) {
					}
				});
		g.PagesTips = g.Page.extend({
					onSubmit : function(a, b) {
					}
				});
		$.ajax({
					type : "post",
					url : "../rest/session/check",
					data : {
						param : paramValue
					},
					dataType : "json",
					success : function(d) {
						MSG.closeAlert();
						var b = d.data;
						var l = b.param;
						channelForeClose = l.channel;
						if (b.result != "success") {
							var c, m;
							m="";
							if (b.result == "expire"){
								m = "链接已过期，请重新点击所需菜单获取相关链接！";
							}
							else if (b.result == "urlerror"){
								m = "链接中参数错误，请重新点击所需菜单获取相关链接！";
							}
							else if (b.result == "SESSION_TIME_OUT"){
								m = "会话已过期，请重新点击所需菜单获取相关链接！";
							}else{
								m = "系统错误，请重新点击所需菜单获取相关链接！";
							}
							var e = {
								TYPE : "A"
							};
							var a = {
								TEXT : "Loading...",
								msg : m
							};
							c = {
								refresh : {
									initVal : a,
									param : e
								}
							};
							f.history_goto("timeOut", c);
						} else {
							myInitMethod(l, j);
						}
					}
				});
	};
});

var MSG = {
		confirmMsg : function(f, i) {	// 提醒用户确认对话框，包含“取消”、“确认”按钮，点“确认”后处理相应业务逻辑
			var j = "";
			MSG.closeAlert();
			j += "<div   class='bg_lock'></div>";
			j += "<div class='alert-box' id='alert-box'>";
			j += "<div class='alert-title'>提示</div>";
			j += this.msgStr(f,i);
			j += "<div class='btn-cancel' onclick='MSG.closeAlert()'>取消</div>";
			j += "<div class='btn-sure' onclick='MSG.success()'>确定</div>";
			j += "</div>";
			$("body").append(j);

			var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
			var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
			if(page_height<$(window).height())
			{
				page_height = $(window).height();	// 屏幕可见尺寸
			}
			if(page_width<$(window).width())
			{
				page_width = $(window).width();
			} 
			$(".bg_lock").height(page_height).width(page_width);
			$(".bg_lock").css("left", 0);
			$(".bg_lock").css("top", 0);
			$(".bg_lock").toggle();
			$("#alert-box").toggle();
		},
		confirmMsgRepay : function(f, i) {	// 提醒用户确认对话框，包含“取消”、“确认”按钮，点“确认”后处理相应业务逻辑
			var j = "";
			MSG.closeAlert();
			j += "<div   class='bg_lock'></div>";
			j += "<div class='alert-box' id='alert-box'>";
			j += "<div class='alert-title'>提示</div>";
			j += this.msgStr(f,i);
			j += "<div class='btn-cancel' onclick='MSG.closeAlert()'>否</div>";
			j += "<div class='btn-sure' onclick='MSG.success()'>是</div>";
			j += "</div>";
			$("body").append(j);

			var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
			var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
			if(page_height<$(window).height())
			{
				page_height = $(window).height();	// 屏幕可见尺寸
			}
			if(page_width<$(window).width())
			{
				page_width = $(window).width();
			} 
			$(".bg_lock").height(page_height).width(page_width);
			$(".bg_lock").css("left", 0);
			$(".bg_lock").css("top", 0);
			$(".bg_lock").toggle();
			$("#alert-box").toggle();
		},
		confirmMsgC : function(f, i) {	// 提醒用户确认对话框，包含“取消”、“确认”按钮，点“确认”后处理相应业务逻辑
			var j = "";
			MSG.closeAlert();
			j += "<div   class='bg_lock'></div>";
			j += "<div class='alert-box' id='alert-box'>";
			j += "<div class='alert-title'>提示</div>";
			j += this.msgStr(f,i);
			j += "<div class='btn-cancel' onclick='MSG.cancel()'>取消</div>";
			j += "<div class='btn-sure' onclick='MSG.success()'>确定</div>";
			j += "</div>";
			$("body").append(j);

			var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
			var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
			if(page_height<$(window).height())
			{
				page_height = $(window).height();	// 屏幕可见尺寸
			}
			if(page_width<$(window).width())
			{
				page_width = $(window).width();
			} 
			$(".bg_lock").height(page_height).width(page_width);
			$(".bg_lock").css("left", 0);
			$(".bg_lock").css("top", 0);
			$(".bg_lock").toggle();
			$("#alert-box").toggle();
		},
		
		alertMsg : function(f, i) {	// 提示信息对话框，只有“关闭”按钮。
			var j = "";
			MSG.closeAlert();
			j += "<div   class='bg_lock'></div>";
			j += "<div class='alert-box' id='alert-box'>";
			j += "<div class='alert-title'>提示</div>";
			j += this.msgStr(f,i);
			j += "<div class='btn-signle' onclick='MSG.closeAlert()'>关闭</div>";
			j += "</div>";
			$("body").append(j);
			var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
			var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
			if(page_height<$(window).height())
			{
				page_height = $(window).height();
			}
			if(page_width<$(window).width())
			{
				page_width = $(window).width();
			} 
			$(".bg_lock").height(page_height).width(page_width);
			$(".bg_lock").css("left", 0);
			$(".bg_lock").css("top", 0);
			$(".bg_lock").toggle();
			$("#alert-box").toggle();
		},
		alertMsgBtn : function(f, i) {	// 提示信息确认框，只有“确认”按钮，点“确认”后处理相应业务逻辑。
			var j = "";
			MSG.closeAlert();
			j += "<div class='bg_lock'></div>";
			j += "<div class='alert-box' id='alertMsgBtn'>";
			j += "<div class='alert-title'>提示</div>";
			j += this.msgStr(f,i);
			j += "<div class='btn-signle' onclick='MSG.success()'>确定</div>";
			j += "</div>";
			$("body").append(j);
			var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
			var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
			if(page_height<$(window).height())
			{
				page_height = $(window).height();
			}
			if(page_width<$(window).width())
			{
				page_width = $(window).width();
			} 
			$(".bg_lock").height(page_height).width(page_width);
			$(".bg_lock").css("left", 0);
			$(".bg_lock").css("top", 0);
			$(".bg_lock").toggle();
			$("#alertMsgBtn").toggle();
		},
		alertMsgBtnShow : function(f, i,m) {	// 提示信息弹出框，只有传入m值文字点击按钮。
			var j = "";
			MSG.closeAlert();
			j += "<div   class='bg_lock'></div>";
			j += "<div class='alert-box' id='alertMsgBtn'>";
			j += "<div class='alert-title'>提示</div>";
			j += this.msgStr(f,i);

			j += "<div class='btn-signle' onclick='MSG.success()'>" + m + "</div>";
			j += "</div>";
			$("body").append(j);
			var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
			var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
			if(page_height<$(window).height())
			{
				page_height = $(window).height();
			}
			if(page_width<$(window).width())
			{
				page_width = $(window).width();
			} 
			$(".bg_lock").height(page_height).width(page_width);
			$(".bg_lock").css("left", 0);
			$(".bg_lock").css("top", 0);
			$(".bg_lock").toggle();
			$("#alertMsgBtn").toggle();
		},
		/*
		 * loadMsgOld : function() { // 页面载入动画 var j = ""; MSG.closeAlert(); j += "<div
		 * class='bg_lock' id='bg_lock' style='background:#fff;'></div>"; j += "<div
		 * class='load_rotate' id='load_rotate'>"; j += "</div>";
		 * $("body").append(j); var page_height = document.body.scrollHeight; //
		 * 页面尺寸，IE下获取结果不正确 var page_width = document.body.scrollWidth; //
		 * 页面尺寸，IE下获取结果不正确
		 * 
		 * if(page_height<$(window).height()) { page_height =
		 * $(window).height(); } if(page_width<$(window).width()) { page_width =
		 * $(window).width(); }
		 * $("#bg_lock").height(page_height).width(page_width);
		 * $("#bg_lock").css("left", 0); $("#bg_lock").css("top", 0);
		 * $("#bg_lock").toggle(); $("#load_rotate").toggle(); var f =
		 * page_height / 2 - $("#load_rotate").height() / 2; var g = page_width /
		 * 2 - $("#load_rotate").width() / 2; $("#load_rotate").css("top", f);
		 * $("#load_rotate").css("left", g); },
		 */
	loadMsg : function() { // 页面载入动画
		var j = "";
		MSG.closeAlert();
		j += "<div class='bg_lock' id='bg_lock'></div>";
		j += "<div class='loadbox' id='load_rotate'>";
		j += "<div class='loadlogo'></div>";
		j += "<div class='spinner'><div class='bounce1'></div><div class='bounce2'></div><div class='bounce3'></div>";
		j += "</div></div>";
		j += "</div>";
		$("body").append(j);
		var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
		var page_width = document.body.scrollWidth;	// 页面尺寸，IE下获取结果不正确
		if(page_height<$(window).height())
		{
			page_height = $(window).height();
		}
		if(page_width<$(window).width())
		{
			page_width = $(window).width();
		} 
		$("#bg_lock").height(page_height).width(page_width);
		$("#bg_lock").css("left", 0);
		$("#bg_lock").css("top", 0);
		$("#bg_lock").toggle();
		$("#load_rotate").toggle();
		var f = page_height / 2 - $("#load_rotate").height() / 2;
		var g = page_width / 2 - $("#load_rotate").width() / 2;
		$("#load_rotate").css("top", f);
		$("#load_rotate").css("left", g);
	},
	closeAlert : function() {	// 弹出层关闭事件，弹出层包括弹出对话框、载入动画、协议页面等
		$("#alert-box").toggle();
		$(".bg_lock").toggle();
		$(".load_rotate").toggle();
		$("#load_rotate").toggle();
		$("#bg_lock").toggle();
		$("#alertMsgBtn").toggle();
		$(".transparentBg").toggle();
		$(".bg_lock").remove();
		$("#alert-box").remove();
		$(".load_rotate").remove();
		$("#load_rotate").remove();
		$("#bg_lock").remove();
		$("#alertMsgBtn").remove();
		$(".transparentBg").remove();
		$("body").unbind("mousedown", MSG.onBodyDown);
	},
	onBodyDown : function() {
		if (!(event.target.id == "alert-box")) {
			MSG.closeAlert();
		}
	},
	viewMsg : function(k) {	// 弹出提示消息，例如协议信息
		var l = "";
		MSG.closeAlert();
		l += "<div id='load_rotate' style='display: none;position: absolute;color:#000;z-index: 2800;'>";
		l += k;
		l += "</div>";
		$("body").append(l);
		$("#load_rotate").toggle();
	},
	msgStr:function(f, i){
		var j = "";
		if(f.length>25)// 计算文字长度是否大于11
		{
			if (!dataUtils.isEmpty(i)) {
				j += "<div class='show-mes'><div class='show-mes-text' style='background:url(\""+i+"\") left top no-repeat;background-size: 28px 28px;'>" + f
						+ "</div></div>";
			} else {
				j += "<div class='show-mes'><span>" + f + "</span></div>";
			}
		}else
		{
			if (!dataUtils.isEmpty(i)) {
				j += "<div class='show-mes show-mes-center'><span class='show-mes-text-center' style='background:url(\""+
					i+"\") no-repeat;background-size: 28px 28px;'>" + f
						+ "</span></div>";
			} else {
				j += "<div class='show-mes show-mes-center'><span>" + f + "</span></div>";
			}
		}
		return j;
	},
	transparent : function(){ 	
		//透明遮罩
		//MSG.closeAlert();
		var page_height = $(window).height();	// 页面尺寸，IE下获取结果不正确
		var page_width = 	$(window).width();  // 页面尺寸，IE下获取结果不正确
		var transparentDiv = $("<div class='transparentBg'></div>");
		transparentDiv.css({
			"width":page_width,
			"height":page_height,
			"position":"absolute",
			"top":0,
			"left":0,
			"z-index":500,
			"display":"block"
			})
		$("body").append(transparentDiv);
	}
};

function toastMessage(b) {	// 弹出吐司信息
	new Toast({
				context : $("body"),
				message : b
			}).show();
}
var Toast = function(b) {
	this.context = b.context == null ? $("body") : b.context;
	this.message = b.message;
	this.time = b.time == null ? 6000 : b.time;
	this.left = b.left;
	this.top = b.top;
	this.init();
};

var msgEntity;
Toast.prototype = {
	init : function() {
		$("#toastMessage").remove();
		var k = new Array();
		k.push('<div id="toastMessage" align="center">');
		k.push("<span  >" + this.message + "</span>");
		k.push("</div>");
		msgEntity = $(k.join("")).appendTo(this.context);

		var page_height = document.body.scrollHeight;	// 页面尺寸，IE下获取结果不正确
		var view_height = document.documentElement.clientHeight;	// 浏览器可见区域高度
		var scroll_height = document.body.scrollTop;// 浏览器滚动距离
	
		var pos = view_height / 2 - $("#toastMessage").height() + page_height - view_height;	// 计算居中位置，但在IE下不能使用
		pos = pos -(page_height-view_height-scroll_height);
		if(pos<0)
		{
			pos = 160;
		}
		msgEntity.css({
					position : "absolute",
					top : pos,
					"z-index" : "2005",
					width : "60%",
					left : "20%",
					"background-color" : "rgba(0,0,0,.8)",
					color : "#fff",
					"font-size" : "20px",
					padding : "14px 0",
					margin : "10px",
					opacity:"0",
					"border-radius":"8px",
					"pointer-events":"none",
					"-webkit-animation-name":"pop-hide",
				});
		msgEntity.hide();
	},
	show : function() {
		msgEntity.focus();
		fadeIn(msgEntity, this.time / 2, null, 5);
		fadeOut(msgEntity, 2500, null, 100);
	}
};

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
var iBase = {
	SetOpacity : function(c, d) {
		c.css("filter", "alpha(opacity=" + d + ")");
		c.css("opacity", d / 100);
	}
};
function fadeIn(f, h, g, i) {
	h = h || 20;
	g = g || 100;
	f.show();
	iBase.SetOpacity(f, 0);
	var j = 0;
	(function() {
		iBase.SetOpacity(f, j);
		j += i;
		if (j <= g) {
			setTimeout(arguments.callee, h);
		}
	})();
}
function fadeOut(f, h, g, i) {
	h = h || 20;
	g = g || 0;
	var j = 100;
	(function() {
		iBase.SetOpacity(f, j);
		j -= i;
		if (j >= g) {
			setTimeout(arguments.callee, h);
		} else {
			if (j < 0) {
				f.hide();
			}
		}
	})();
};

var dataUtils = {
		isEmpty : function(b) {
			if (b == null) {
				return true;
			}
			if ((typeof(b) == "undefined")) {
				return true;
			} else {
				if (typeof(b) == "string") {
					if (b == "undefined") {
						return true;
					}
				}
			}
			if (b.trim() == "") {
				return true;
			}
			return false;
		},
		timeStamp : function() {
			var c = new Date();
			var d = "";
			d = c.getFullYear() + "";
			d = d + (c.getMonth() + 1) + "";
			d = d + c.getDate() + "";
			d = d + c.getHours() + "";
			d = d + c.getMinutes() + "";
			d = d + c.getSeconds() + "";
			return d;
		},
		isChinese : function(d) {
			var c = /[u00-uff]/;
			return !c.test(d);
		},
		isNumOrEn : function(d) {
			var c = /^[A-Za-z0-9]+$/;
			return c.test(d);
		},
		isDigit : function(d) {
			var c = /^[0-9]+$/;
			return c.test(d);
		},
		isHuzhao : function(c) {
			var d = /^[A-Za-z0-9]+$/;
			return d.test(c);
		}
	};
/**
 * 关闭页面
 */
function closeButton() {	// 关闭页面
	
	if (channelForeClose == config.channl.zfb) {// 支付宝
		AlipayJSBridge.call('closeWebview');
	} else {
		WeixinJSBridge.call('closeWindow');
	}
}


/**
 * 替换任何非数字字符,返回数字字符串
 * @param str
 * @returns
 */
function onlyNum(str){
	var reg=/[^0-9]/gi;
	return str.replace(reg,'');
}

/**
 * 控制职能输入数字和小数点,且小数点只能保留后两位 小数点不能在第一位
 * @param e
 * @returns {Boolean}
 */
function noNumbers(e) { 
	var keynum  ;
	var keychar  ;
	var numcheck  ;
	if(window.event) // IE  
	{  
		keynum = e.keyCode  
	}else if(e.which) // Netscape/Firefox/Opera  
	{  
	keynum = e.which  
	}  
	keychar = String.fromCharCode(keynum);
	//判断是数字,且小数点后面只保留两位小数
	if(!isNaN(keychar)){
		var index=e.currentTarget.value.indexOf(".");
		if(index >= 0 
				&& e.currentTarget.value.length-index >2){
			return false;
		}
		return true;
	}
	//如果是小数点 但不能出现多个 且第一位不能是小数点
	if("."== keychar ){
		if(e.currentTarget.value==""){
			return false;
		}
		if(e.currentTarget.value.indexOf(".") >= 0){
			return false;
		}
		return true;
	}
	return false  ;
}  




/**
 * 保留卡的前四位后四位
 * @param cardNo
 * @returns {String}
 */
function replaceAccNo(cardNo){
	if(cardNo!=null && cardNo!="" &&cardNo.length>0){
		if(cardNo.length>8){
			var s="";
			for(var i=0;i<(cardNo.length-8);i++){
				s=s+"*";
			}
			cardNo=cardNo.substring(0,4)+s+cardNo.substring((cardNo.length-4),cardNo.length);
		}
		
	}
	return cardNo;
}

/**
 * 截取卡的后四位
 * @param cardNo
 * @returns
 */
function sbAccNO(cardNo){
	if(cardNo!=null && cardNo!="" &&cardNo.length>0){
		if(cardNo.length>4){
		cardNo=cardNo.substring((cardNo.length-4),cardNo.length);
		}
	}
	return cardNo;
}

/**
 * 判断金额是否为空 为空返回0.00
 * @param ACCBi
 * @returns {String}
 */
function replaceNum(ACCBi){
	if(ACCBi=="" || ACCBi==null){
		ACCBi="0.00";
	}
	return ACCBi;
}

/**
 * 去掉空格
 */
function replaceSpace(str){
	return str.replace(/ /g,'');
}
/**
 * 隐藏卡号倒数第五位开始的4位 
 */
function repFourAcc(cardNo){
var card="";	
	if(cardNo!=null && cardNo!="" &&cardNo.length>0){
		if(cardNo.length>12){
			var s="";
			for(var i=0;i<4;i++){
				s=s+"*";
			}
			cardNo=cardNo.substring(0,(cardNo.length-8)) + s + cardNo.substring((cardNo.length-4),cardNo.length);			
		}
		
	}
	return cardNo;
}

